<template>
  <div>
    <div class="outer">
      <h2>List</h2>
      <button @click="sendDataToParent">向父组件发送数据</button>
      <button @click="destroyList">卸载list组件</button>
    </div>
  </div>
</template>

<script>
export default {
  name: "List",
  data() {
    return {
      list: [
        { name: "xiaoming", age: 18 },
        { name: "xiaowang", age: 19 },
        { name: "xiaoli", age: 20 },
      ],
    };
  },
  methods: {
    sendDataToParent() {
      //this.$emit触发当前实例上的自定义事件。附加参数都会传给监听器回调。
      this.$emit("atguigu", this.list);
    },
    destroyList() {
      //卸载list组件
      this.$destroy();
    },
  },
  beforeDestroy() {
    /* 
      真正的解绑:
        是在被绑定自定义事件的子组件中卸载调用自定义事件
    */
    this.$off("atguigu");
  },
};
</script>

<style scoped>
.outer {
  height: 200px;
  margin: 30px;
  background: pink;
}
</style>